# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import os

# 获取所有文件名
def get_files():
    file_name = open('file_name.txt', 'w')
    file_list = open('file_list.txt', 'w')
    for filepath,dirnames,filenames in os.walk(os.getcwd()):
        for filename in filenames:
            file_name.write(filename+'\r')
            file_list.write(os.path.join(filepath,filename)+'\r')
            # print(os.path.join(filepath,filename))
    file_name.close()
    file_list.close()

# 分离出所有.c
def find_c():
    file_list = open('file_list.txt', 'r')
    file_list_c = open('file_list_c.txt', 'w')
    line = 1
    while line:
        line = file_list.readline()
        if (line.find(".c",-3)!=-1):
            file_list_c.write(line)
            # print(line)
    file_list.close()
    file_list_c.close()

    file_name = open('file_name.txt', 'r')
    file_name_c = open('file_name_c.txt', 'w')
    line = 1
    while line:
        line = file_name.readline()
        if (line.find(".c", -3) != -1):
            file_name_c.write(line)
            # print(line)
    file_name.close()
    file_list_c.close()

def file_c_to_o():
    file_list_c = open('file_list_c.txt', 'r')
    file_list_c_to_o = open('file_list_c_to_o.txt', 'w')
    line = 1
    while line:
        line = file_list_c.readline()
        line_o = line.replace(".c", ".o")
        file_list_c_to_o.write(line_o)
    file_list_c.close()
    file_list_c_to_o.close()

    file_name_c = open('file_name_c.txt', 'r')
    file_name_c_to_o = open('file_name_c_to_o.txt', 'w')
    line = 1
    while line:
        line = file_name_c.readline()
        line_o = line.replace(".c", ".o")
        file_name_c_to_o.write(line_o)
    file_name_c.close()
    file_name_c_to_o.close()

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print("作者：Z_Tam")
    print("Blog：http://z-tam.cn")
    print("Giee：https://gitee.com/Z_Tam/python-tools\r\n")
    print("start")
    get_files()
    find_c()
    file_c_to_o()
    print("finish")
